package architect.design.patterns.creation.singleton;

import java.io.*;

/**
 * 序列化单例
 *
 * @author guanxin
 * @date 2018/5/4 10:54
 */

public class SerializableSingletonCase {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        SerializableSingleton instanceOne = SerializableSingleton.getInstance();
        ObjectOutput out = new ObjectOutputStream(new FileOutputStream(
                "filename.ser"));
        out.writeObject(instanceOne);
        out.close();

        // 从文件反序列化到对象
        ObjectInput in = new ObjectInputStream(new FileInputStream(
                "filename.ser"));
        SerializableSingleton instanceTwo = (SerializableSingleton) in.readObject();
        in.close();

        System.out.println("instanceOne hashCode=" + instanceOne.hashCode());
        System.out.println("instanceTwo hashCode=" + instanceTwo.hashCode());
    }
}
